﻿using Architecture.Domain.Service;
using Architeture.Domain;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using RBAC.ZhuBoLin.Application.Command.RoleCommand;
using RBAC.ZhuBoLin.Application.Command.RoleCommand.Role;
using RBAC.ZhuBoLin.Dtos;
using System.Security.Claims;

namespace RBAC.ZhuBoLin.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class RoleController : ControllerBase
    {
        protected readonly IRoleService roleservice;
        protected readonly IMediator mediator;


        /// <summary>
        /// 用户角色添加
        /// </summary>
        /// <param name="a"></param>
        /// <returns></returns>
        /// 
        [HttpPost]
        public async Task<ApiResult<int>> RoleAdd(RoleCreateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                //获取当前登录用户的所有的token的信息
                var data = HttpContext.User;
                List<int> ls = new List<int>();
                var a = ls.Count();
                //过去sub的值
                var id = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
                //获取角色的值
                var role = HttpContext.User.FindFirst(ClaimTypes.Role)?.Value;
                //用户自定义的数据
                var userdata = HttpContext.User.FindFirst(ClaimTypes.UserData)?.Value;
                result.Data = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = 1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 用户角色显示
        /// </summary>
        /// <returns></returns>

        [HttpGet]

        public async Task<ApiResult<page>> RoleGetAll([FromQuery] RoleSelectCommand com)
        {
            ApiResult<page> result = new ApiResult<page>();
            try
            {
                result.Data = await mediator.Send(com);
            }
            catch (Exception ex)
            {
                result.Code = 1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 用户角色删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// 
        [HttpPut]
        public async Task<ApiResult<int>> RoleRemove([FromQuery] RoleDeleteCommand cd)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await mediator.Send(cd);
            }
            catch (Exception ex)
            {
                result.Code = 1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        [HttpGet]
        public async Task<ApiResult<RoleInfo>> RoleGetById([FromQuery] RoleSetCommand cd)
        {
            ApiResult<RoleInfo> result = new ApiResult<RoleInfo>();
            try
            {
                result.Data = await mediator.Send(cd);
            }
            catch (Exception ex)
            {
                result.Code = 1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="a"></param>
        /// <returns></returns>
        /// 
        [HttpPut]
        public async Task<ApiResult<int>> RoleUpd(RoleUpdateCommand a)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await mediator.Send(a);
            }
            catch (Exception ex)
            {
                result.Code = 1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        public RoleController(IRoleService roleservice, IMediator mediator)
        {
            this.roleservice = roleservice;
            this.mediator = mediator;
        }


        /// <summary>
        /// 角色全部显示
        /// </summary>
        /// <returns></returns>
        /// 
        [HttpGet]
        public async Task<ApiResult<object>> GetRoleInfo()
        {
            ApiResult<object> result = new ApiResult<object>();
            try
            {
                result.Data = await mediator.Send(new RoleInfoGetAllCommand());
            }
            catch (Exception ex)
            {
                result.Code = 1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
    }
}
